﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;

public partial class ajax_getproject : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] == null || Session["permission"] == null || ((int)Session["permission"]) <= 1 ||
            ((int)Session["permission"]) >= 4)
        {
            Response.Write("FAIL");
            return;
        }

        // check current user is enable to access current project
        SqlParameter[] listParams = new SqlParameter[3];
        //-- param 1: @userId
        int userId = Int32.Parse(Session["id_user"].ToString());
        SqlParameter param = new SqlParameter("@userId", userId);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        listParams[0] = param;

        //-- param 2: @projectId
        int projectId = Int32.Parse(Request.QueryString["id"]);
        param = new SqlParameter("@projectId", projectId);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        listParams[1] = param;

        //-- param 3: @isAccess
        param = new SqlParameter();
        param.ParameterName = "@isAccess";
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Output;
        listParams[2] = param;

        dal.ExecuteSP("proCheckAccessProject_GET", listParams);
        int enable = Int32.Parse(listParams[2].Value.ToString());
        if (enable == 0)
        {
            Response.Write("FAIL");
            return;
        }

        try
        {
            string sql = "SELECT name,description,priority,[public],convert(varchar,start,103) as 'start', convert(varchar,[end],103) as 'end' FROM project "+
                         "WHERE id=  " + comm.to_sql(projectId.ToString(), "number");

            DataTable dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count != 1)
            {
                Response.Write("FAIL");
                return;
            }
            else
            {
                string json = "";// "{";
                //DataRow row = dtTable.Rows[0];
                //json +="'name':'" + row["name"].ToString()+"',";
                //json += "'description':'" + row["description"].ToString() + "',";
                //json += "'start':'" + row["start"].ToString() + "',";
                //json += "'end':'" + row["end"].ToString() + "',";
                //json += "'public':'" + row["public"].ToString() + "',";
                //json += "'priority':'" + row["priority"].ToString() + "'}";
                json = JsonConvert.SerializeObject(dtTable.Rows[0].Table);
                Response.Write(json);
                return;
            }
        }
        catch
        {
            Response.Write("FAIL");
            return;
        }
    }
}
